﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Rem.Objects;
using System.Data.Objects;
using FileDB;
namespace Rem.Objects.Forms
{
    public partial class UcContract2 : BaseUserControl
    {
        public UcContract2()
        {
            InitializeComponent();
            if (this.Data == null)
            {
                this.Data = new Contract()
                {
                    Id = QueryConstants.UnsavedObjectId,

                    RentInterval = 3,
                    RentPerInterval = 10000,
                    Deposit = 10000 * 3,
                    Address = "上海市",
                    AdvancedRenew = 20,
                    AdvancedDays = 5,
                    //PartAId = 1,
                    //PartBId = 1,
                    ContractNo = DateTime.Now.ToString("yyyy-MM-dd"),
                    StartDate = DateTime.Now,
                    DueDate = DateTime.Now.AddYears(1).AddDays(-1)
                };
            }


        }

        private void DisplayData()
        {
            this.addressTextBox.Text = contract.Address;
            this.contractNoTextBox.Text = contract.ContractNo;
            this.areaSizeNumericUpDown.Value = (decimal)(contract.AreaSize);
            this.widthNumericUpDown.Value = (decimal)(contract.Width);

            this.rentNumericUpDown.Value = (decimal)contract.RentPerInterval;
            this.rentIntervalNumericUpDown.Value = contract.RentInterval;
            this.depositNumericUpDown.Value = (decimal)contract.Deposit;
            this.advancedDaysNumericUpDown.Value = contract.AdvancedDays;


            this.freeDaysInNumericUpDown.Value = contract.FreeDaysIn;
            this.freeDaysOutNumericUpDown.Value = contract.FreeDaysOut;

            this.startDateDateTimePicker.Value = contract.StartDate;
            this.dueDateDateTimePicker.Value = contract.DueDate;
            this.nudRenew.Value = (decimal)contract.AdvancedRenew;
            //this.signTimeDateTimePicker.Value = contract.SignTime.HasValue ? contract.SignTime.Value : DateTime.Now;

            this.commentTextBox.Text = contract.Comment;

            if (this.contract.Id > 0)
            {
                SetButtons(true);
            }
            else
            {
                SetButtons(false);
            }
            if (this.contract != null && this.contract.Id > 0)
            {
                if (this.contract.CustomerPartA != null)
                {
                    this.partANameLinkLabel.Text = this.contract.CustomerPartA.Name;
                    this.partANameLinkLabel.Tag = this.contract.CustomerPartA;
                }
                if (this.contract.CustomerPartB != null)
                {
                    this.partBNameLinkLabel.Text = this.contract.CustomerPartB.Name;
                    this.partBNameLinkLabel.Tag = this.contract.CustomerPartB;
                }

                //if (this.contract.Employee != null)
                //{
                //    this.employeeNameLinkLabel1.Text = this.contract.Employee.Name;
                //    this.employeeNameLinkLabel1.Tag = this.contract.Employee;
                //}
            }
            else
            {
                //if((from c in BaseForm.RemDatabaseEntities.Customers
                //    where this.contract.PartAId == c.Id).Count() == 1)
                //{
                //    this.partANameLinkLabel.Text = this.contract.CustomerPartA.Name;
                //    this.partANameLinkLabel.Tag = this.contract.CustomerPartA;
                //}
            }
            //if (SubmitRentDateMapping.Keys.Contains(this.contract.SubmitRentDate))
            //{
            //submitRentDateComboBox.SelectedText = SubmitRentDateMapping[this.contract.SubmitRentDate];
            //}

            //submitRentDateComboBox.SelectedValue = this.contract.SubmitRentDate;
            lblID.Text = string.Format(QueryConstants.IDTemplate, contract.Id.ToString());
            this.toolStripButton1.Text = this.contract.Status == null || this.contract.Status.Contains("启用") ? "禁用" : "启用";

        }

        private void SetButtons(bool enable)
        {
            this.reminderPeriodBuildbtn.Enabled = enable;
            this.button2.Enabled = enable;
            this.button3.Enabled = enable;
            this.button7.Enabled = enable;

            this.button4.Enabled = enable;
            this.button5.Enabled = enable;
            this.button6.Enabled = enable;
        }

        private void BindingData()
        {
            //contractBindingSource.DataSourceChanged += (sender, arg) =>
            //{
            //    DisplayAddtionalInfo();
            //};
            //contractBindingSource.DataSource = this.contract;


            //submitRentDateComboBox.DataSource = SubmitRentDateMapping.Select((pair) =>
            //{
            //    return new { Key = pair.Key, Value = pair.Value };
            //}).ToList();
            //submitRentDateComboBox.DisplayMember = "Value";
            //submitRentDateComboBox.ValueMember = "Key";
        }

        public Dictionary<int, string> SubmitRentDateMapping
        {
            get
            {
                Dictionary<int, string> result = new Dictionary<int, string>();
                for (int i = 1; i < 29; i++)
                {
                    result.Add(i, string.Format("第{0}天", i));
                }
                for (int i = -3; i < 0; i++)
                {
                    result.Add(i, string.Format("倒数第{0}天", Math.Abs(i)));
                }

                return result;
            }
        }
        private Contract contract { get { return this.Data as Contract; } }
        public override Object Data
        {
            get
            {
                return base.Data;
            }
            set
            {
                //todo: add event instead of the code
                Contract contract = value as Contract;
                if (contract == null)
                {
                    throw new Exception("object can't set to null");
                }
                base.Data = value;
                DisplayData();
            }
        }


        #region addtional info
        private void NewAddtionalInfo(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(this.addtionalContentTextBox.Text))
            {
                return;
            }

            if (string.IsNullOrWhiteSpace(this.addtionalValueTextBox.Text))
            {
                return;
            }
            //float addtionalValue;

            //if (!float.TryParse(this.addtionalValueTextBox.Text, out addtionalValue))
            //{
            //    return;
            //}

            //bool contentExisting = false;

            //foreach (var source in contract.ContractAddtionalInfoes)
            //{
            //    if (source.Name == this.addtionalContentTextBox.Text)
            //    {
            //        contentExisting = true;
            //    }
            //}

            //if (contentExisting)
            //{
            //    MessageBox.Show(string.Format("附加信息项{0}已存在,请确认填写唯一附加项内容！", addtionalContentTextBox.Text));
            //    return;
            //}

            //ContractAddtionalInfo addtionalInfo = new ContractAddtionalInfo()
            //{
            //    Id = QueryConstants.UnsavedObjectId,
            //    Name = this.addtionalContentTextBox.Text,
            //    Value = this.addtionalValueTextBox.Text,
            //    ContractId = contract.Id
            //};

            //addtionalInfo.Save(BaseForm.RemDatabaseEntities);
            //contract.ContractAddtionalInfoes.Add(addtionalInfo);
            //this.contractAddtionalInfoesDataGridView.Refresh();
        }

        private void UpdateAddtioinalInfo(object sender, EventArgs e)
        {
            //if (this.contractAddtionalInfoesDataGridView.SelectedRows.Count == 1)
            //{
            //    ContractAddtionalInfo info = contractAddtionalInfoesDataGridView.SelectedRows[0].DataBoundItem as ContractAddtionalInfo;
            //    info.Name = this.addtionalContentTextBox.Text;
            //    info.Value = this.addtionalValueTextBox.Text;
            //    info.Save(BaseForm.RemDatabaseEntities);
            //}
        }

        private void DeleteAddtioinalInfo(object sender, EventArgs e)
        {
            //if (this.contractAddtionalInfoesDataGridView.SelectedRows.Count == 1)
            //{
            //    ContractAddtionalInfo info = contractAddtionalInfoesDataGridView.SelectedRows[0].DataBoundItem as ContractAddtionalInfo;
            //    if (info.Delete(BaseForm.RemDatabaseEntities) > 0)
            //    {
            //        this.contractAddtionalInfoesDataGridView.Rows.RemoveAt(contractAddtionalInfoesDataGridView.SelectedRows[0].Index);
            //        this.contractAddtionalInfoesDataGridView.Refresh();
            //    }
            //    else
            //    {
            //        MessageBox.Show("删除数据失败！", "数据删除", MessageBoxButtons.OK);
            //    }
            //}
        }
        #endregion

        private void saveToolStripButton_Click(object sender, EventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                Save();
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }
        public override bool Save()
        {
            #region validate data
            if (string.IsNullOrWhiteSpace(this.addressTextBox.Text))
            {
                MessageBox.Show("请填写地址信息！");
                return false;
            }

            if (string.IsNullOrWhiteSpace(this.contractNoTextBox.Text))
            {
                MessageBox.Show("请填写合同编号！");
                return false;
            }

            if (this.dueDateDateTimePicker.Value <= this.startDateDateTimePicker.Value)
            {
                MessageBox.Show("结束日期没有大于开始日期！");
                return false;
            }

            //Employee tempEmployee = this.employeeNameLinkLabel1.Tag as Employee;
            //if (tempEmployee == null || tempEmployee.Id <= 0)
            //{
            //    if (string.IsNullOrWhiteSpace(this.employeeNameLinkLabel1.Text))
            //    {
            //        MessageBox.Show("请填写员工信息！");
            //        return false;
            //    }
            //    tempEmployee = new Employee() { Id = QueryConstants.UnsavedObjectId, Name = this.employeeNameLinkLabel1.Text };
            //    tempEmployee.Save(BaseForm.RemDatabaseEntities);
            //    this.employeeNameLinkLabel1.Tag = tempEmployee;
            //}

            Customer partA = this.partANameLinkLabel.Tag as Customer;
            if (partA == null || partA.Id <= 0)
            {
                if (string.IsNullOrWhiteSpace(this.partANameLinkLabel.Text))
                {
                    MessageBox.Show("请填写收租方客户信息！");
                    return false;
                }
                //this.contract.CustomerPartA = new Customer() { Id = QueryConstants.UnsavedObjectId, Name = this.partANameLinkLabel.Text };
                //this.contract.CustomerPartA.Save(BaseForm.RemDatabaseEntities);
                //this.contract.PartAId = this.contract.CustomerPartA.Id;
                //partA = this.contract.CustomerPartA;
                partA = new Customer() { Id = QueryConstants.UnsavedObjectId, Name = this.partANameLinkLabel.Text };
                Customer.Insert(partA);
                this.partANameLinkLabel.Tag = partA;
            }

            Customer partB = this.partBNameLinkLabel.Tag as Customer;
            if (partB == null || partB.Id <= 0)
            {
                if (string.IsNullOrWhiteSpace(this.partBNameLinkLabel.Text))
                {
                    MessageBox.Show("请填写缴租方客户信息！");
                    return false;
                }
                //this.contract.CustomerPartB = new Customer() { Id = QueryConstants.UnsavedObjectId, Name = this.partBNameLinkLabel.Text };
                //this.contract.CustomerPartB.Save(BaseForm.RemDatabaseEntities);
                //this.contract.PartBId = this.contract.CustomerPartB.Id;
                partB = new Customer() { Id = QueryConstants.UnsavedObjectId, Name = this.partBNameLinkLabel.Text };
                Customer.Insert(partB);
                this.partBNameLinkLabel.Tag = partB;
            }
            #endregion

            #region assgin data
            this.contract.SignTime = this.signTimeDateTimePicker.Value;

            this.contract.Address = this.addressTextBox.Text;
            this.contract.ContractNo = this.contractNoTextBox.Text;
            this.contract.AreaSize = this.areaSizeNumericUpDown.Value;
            this.contract.Width = this.widthNumericUpDown.Value;


            this.contract.Deposit = (double)(this.depositNumericUpDown.Value);
            this.contract.RentInterval = (int)this.rentIntervalNumericUpDown.Value;
            //this.contract.SubmitRentDate = (int)this.submitRentDateComboBox.SelectedValue;


            this.contract.RentPerInterval = (double)this.rentNumericUpDown.Value;
            this.contract.AdvancedDays = (int)this.advancedDaysNumericUpDown.Value;

            this.contract.AdvancedRenew = (int)this.nudRenew.Value;
            this.contract.StartDate = this.startDateDateTimePicker.Value;
            this.contract.DueDate = this.dueDateDateTimePicker.Value;
            this.contract.FreeDaysIn = (int)this.freeDaysInNumericUpDown.Value;
            this.contract.FreeDaysOut = (int)this.freeDaysOutNumericUpDown.Value;
            this.contract.Comment = this.commentTextBox.Text;

            //BaseForm.RemDatabaseEntities.AddToContracts(this.contract);
            int? objectId = null;
            if (this.contract.Id > 0)
            {
                objectId = contract.Id;
            }
            #endregion


            //ObjectResult<Nullable<global::System.Int32>> result = BaseForm.RemDatabaseEntities.ContractAddUpdate(objectId,
            //    this.contract.Address, this.contract.AreaSize, partA.Id,
            //    tempEmployee.Id, contract.SignTime, contract.Rent, contract.RentInterval, contract.RentPerInterval,
            //    contract.Deposit, contract.AdvancedDays, contract.FreeDaysIn, contract.FreeDaysOut, contract.StartDate,
            //    contract.DueDate, contract.Comment, contract.ContractNo, contract.ReminderMessageTemplateId,
            //    partB.Id, contract.Width, chbCanTicket.Checked ? "是" : "否", (int)nudRenew.Value);
            //this.contract.Id = result.Single<int?>().Value;

            //this.contract.Employee = tempEmployee;
            //this.contract.CustomerPartA = partA;
            //this.contract.CustomerPartB = partB;
            //this.contract.EmployeeId = tempEmployee.Id;
            this.contract.PartAId = partA.Id;
            this.contract.PartBId = partB.Id;

            DisplayData();
            return base.Save();
        }
        private IEnumerable<Reminder> _reminders;

        public IEnumerable<Reminder> Reminders
        {
            get { return _reminders; }
            set { _reminders = value; }
        }

        public IEnumerable<Reminder> ContractReminder { get; set; }
        private void reminderSettingTabPage_Enter(object sender, EventArgs e)
        {
            if (this.reminderSettingTabPage.Tag == null)
            {
                UcDataGrid.InitializateGridView(this.reminderDataGridView, this.contract.Reminders,
                    QueryConstants.ReminderFieldNameHeadTextMapping, QueryConstants.ReminderInvisiableFieldNames);
                this.reminderSettingTabPage.Tag = new object();
            }
        }

        private void contractAddtionalInfoesDataGridView_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            //if (contractAddtionalInfoesDataGridView.SelectedRows.Count == 1)
            //{
            //    ContractAddtionalInfo info = contractAddtionalInfoesDataGridView.SelectedRows[0].DataBoundItem as ContractAddtionalInfo;
            //    if (info != null)
            //    {
            //        this.addtionalContentTextBox.Text = info.Name;
            //        this.addtionalValueTextBox.Text = info.Value.ToString();
            //    }
            //}
        }

        private void fixMessageCheckbox_CheckedChanged(object sender, EventArgs e)
        {

        }

        #region reminder setting

        private void reminderPeriodBuildbtn_Click(object sender, EventArgs e)
        {
            ReminderForm2 form = new ReminderForm2(new Reminder() { ContractId = contract.Id } ) 
            { Text = string.Format("生成提醒#合同{0}", contract.ContractNo) };

            form.Dock = DockStyle.Fill;
            form.periedgroupBox.Visible = true;

            form.GenerateReminders += (source, arg) =>
            {
                //BaseForm.RemDatabaseEntities.Refresh(RefreshMode.StoreWins, 
                //    BaseForm.RemDatabaseEntities.Reminders);
                //IEnumerable<Reminder> reminders = source as IEnumerable<Reminder>;
                //if (reminders != null)
                //{
                //    foreach (var reminder in reminders)
                //    {
                //        this.contract.Reminders.Add(reminder);
                //    }
                //}

                IEnumerable<Reminder> reminders = source as IEnumerable<Reminder>;
                if (reminders != null)
                {
                    //this.contractAddtionalInfoesDataGridView.Refresh();
                    //this.Reminders.ToList().AddRange(reminders);
                    //this.contractAddtionalInfoesDataGridView.Refresh();
                    QueryForm.OpenReminderQuery(0, "合同周期提醒", reminders);
                }
                this.Focus();
                form.saveToolStripButton.Enabled = false;
            };
            form.Show();
        }

        private void reminderRemove(object sender, EventArgs e)
        {
            if (this.reminderDataGridView.SelectedRows.Count == 1)
            {
                if (MessageBox.Show("确定删除吗？", "数据删除", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
                {
                    return;
                }
                Reminder info = reminderDataGridView.SelectedRows[0].DataBoundItem as Reminder;
                //if (info.Delete(BaseForm.RemDatabaseEntities) > 0)
                    if(true)
                {
                    this.reminderDataGridView.Rows.RemoveAt(reminderDataGridView.SelectedRows[0].Index);
                    this.contractAddtionalInfoesDataGridView.Refresh();
                }
                else
                {
                    MessageBox.Show("删除数据失败！", "数据删除", MessageBoxButtons.OK);
                }
            }
        }

        private void reminderNew(object sender, EventArgs e)
        {
            ReminderForm2 form = new ReminderForm2(new Reminder() { ContractId = contract.Id }) { Text = string.Format("生成提醒#合同{0}", contract.ContractNo) };
            form.periedgroupBox.Visible = false;

            form.GenerateReminders += (source, arg) =>
            {
                IEnumerable<Reminder> reminders = source as IEnumerable<Reminder>;
                if (reminders != null && reminders.Count() == 1)
                {
                    this.contract.Reminders.Add(reminders.Single());
                }
                this.Focus();
                form.saveToolStripButton.Enabled = false;
                form.Close();
            };
            form.Show();
        }

        private void reminderUpdate(object sender, EventArgs e)
        {
            if (this.contractAddtionalInfoesDataGridView.SelectedRows.Count == 1)
            {
                Reminder info = reminderDataGridView.SelectedRows[0].DataBoundItem as Reminder;
                ReminderForm2 form = new ReminderForm2(info);
                form.Show();
            }
        }

        private void reminderSave(object sender, EventArgs e)
        {
        }
        #endregion

        #region customer and employee pick and link click
        private void custormNameLinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Customer customer = (sender as LinkLabel).Tag as Customer;
            if (customer != null && customer.Id > 0)
            {
                CustomerForm form = new CustomerForm(customer);
                form.Show();
            }
            else
            {
                CustomerForm form = new CustomerForm() ;
                form.Show();
            }
        }

        private void employeeNameLinkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            //Employee employee = this.employeeNameLinkLabel1.Tag as Employee;
            //if (employee != null && employee.Id > 0)
            //{
            //    EmployeeForm form = new EmployeeForm(employee);
            //    form.Show();
            //}
            //else
            //{
            //    EmployeeForm form = new EmployeeForm();
            //    form.Show();
            //}
        }

        #region select customer
        private void selectCustormerBtn_Click(object sender, EventArgs e)
        {
            SelectCustomer(partANameLinkLabel);
        }

        private void button11_Click(object sender, EventArgs e)
        {
            SelectCustomer(partBNameLinkLabel);
        }

        private void SelectCustomer(LinkLabel customerLabel)
        {
            ObjectPickerForm picker = new ObjectPickerForm(BaseObjectTypes.Customer);
            picker.ShowDialog();
            if (picker.DialogResult == DialogResult.OK)
            {
                Customer customer = picker.SelectedObject as Customer;
                if (customer != null)
                {
                    customerLabel.Text = customer.Name;
                    customerLabel.Tag = customer;
                }
            }
        }
        #endregion

        private void selectEmployeeBtn_Click(object sender, EventArgs e)
        {
            //ObjectPickerForm picker = new ObjectPickerForm(BaseObjectTypes.Employee);
            //picker.ShowDialog();
            //if (picker.DialogResult == DialogResult.OK)
            //{
            //    Employee employee = picker.SelectedObject as Employee;
            //    if (employee != null)
            //    {
            //        this.employeeNameLinkLabel1.Text = employee.Name;
            //        this.employeeNameLinkLabel1.Tag = employee;
            //    }
            //}
        }
        #endregion


        #region UpdateRentPerInterval
        private void rentIntervalNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            UpdateRentPerInterval();
        }

        private void rentNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            UpdateRentPerInterval();

        }

        private void UpdateRentPerInterval()
        {
            // float rent = (float)this.rentNumericUpDown.Value;
            // this.rentPerIntervalTextBox.Text = ((float)(rent / (int)this.rentIntervalNumericUpDown.Value)).ToString();
        }
        #endregion


        private void tabPage2_Enter(object sender, EventArgs e)
        {
            //if (this.tabPage2.Tag == null)
            //{
            //    UcDataGrid.InitializateGridView(
            //    contractAddtionalInfoesDataGridView,
            //    this.contract.ContractAddtionalInfoes,
            //     QueryConstants.ContractAdditionalInfoFieldNameHeadTextMapping,
            //     QueryConstants.ContractAdditionalInfoInvisiableFieldNames);
            //    this.tabPage2.Tag = new object();
            //}
        }

        #region
        private void newToolStripButton_Click(object sender, EventArgs e)
        {
            (new ContractForm()).Show();
        }



        #endregion

        private void openToolStripButton_Click(object sender, EventArgs e)
        {
           var ids = GoToForm.GetID("Id", "Address", BaseObjectTypes.Contract);
           int objectId = ids.Count == 1 ? ids[0] : QueryConstants.UnsavedObjectId;
            if (objectId > 0)
            {
                Contract contract = (from c in RemDatabaseEntities.Contracts
                                     where c.Id == objectId
                                     select c
                                     ).SingleOrDefault();
                if (contract != null && contract.Id > 0)
                {
                    ContractForm f = new ContractForm(contract);
                    f.Show();
                    f.Dock = DockStyle.Fill;
                }
            }
        }

        private void reminderDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void cloneToolStripButton_Click(object sender, EventArgs e)
        {

        }

        private void deleteToolStripButton_Click(object sender, EventArgs e)
        {
            if (this.contract.Id <= 0)
            {
                return;
            }
            DialogResult result = MessageBox.Show("确定删除此合同！", "合同删除", MessageBoxButtons.YesNo);
            try
            {
                if (result == DialogResult.Yes)
                {
                    this.Cursor = Cursors.WaitCursor;
                    try
                    {
                        //if (this.contract.Delete(BaseForm.RemDatabaseEntities))
                        if(true)
                        {
                            if (base.Deleted != null)
                            {
                                base.Deleted(this, null);
                            }
                        }
                        else
                        {
                            MessageBox.Show("删除出错或数据不存在！", "合同删除", MessageBoxButtons.OK);
                        }
                    }
                    catch (InvalidOperationException invalidException)
                    {
                        MessageBox.Show("删除出错！", "合同删除", MessageBoxButtons.OK);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("已有数据关联到此合同！", "合同删除", MessageBoxButtons.OK);
                    }
                }

            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void UcContract2_Load(object sender, EventArgs e)
        {
            BindingData();
            DisplayData();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            try
            {

                this.Cursor = Cursors.Arrow;
                if (this.toolStripButton1.Text.Contains("禁用"))
                {
                    if (MessageBox.Show(string.Format("确定{0}此合同?{1}", this.toolStripButton1.Text, "未发生提醒数据将会删除！"), "合同更改", MessageBoxButtons.YesNo) == DialogResult.No)
                    {
                        return;
                    }

                    
                    Contract.ContractEnable(this.contract.Id, false);
                    this.toolStripButton1.Text = "启用";
                }
                else if (this.toolStripButton1.Text.Contains("启用"))
                {
                    Contract.ContractEnable(this.contract.Id, true);
                    this.toolStripButton1.Text = "禁用";
                }


            }
            catch (Exception ex)
            {
                MessageBox.Show("操作出错！");
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }



        private void toolStripButton2_Click_1(object sender, EventArgs e)
        {
            this.Data = (from c in RemDatabaseEntities.Contracts
                         where c.Id == this.contract.Id
                         select c).Single();
            UcDataGrid.InitializateGridView(this.reminderDataGridView, this.contract.Reminders,
            QueryConstants.ReminderFieldNameHeadTextMapping, QueryConstants.ReminderInvisiableFieldNames);

            this.reminderDataGridView.Refresh();
        }
    }
}